<?php

class Admin_ContentController extends CMS_Controller_Action_Admin {

    public function init() {
        parent::init();
    }

    public function indexAction() {
        $lang = Common_Translate::getLanguage();
        $categories = new CMS_Models_LangContentCategory();
        $this->view->categories = $categories->getCategoriesByLang($lang);
    }

    public function newcategoryAction() {
        $form = $this->addCategoryForm();
        $this->view->form = $form;
        $langs = new CMS_Models_Lang();
        $this->view->langs = $langs->getEnabledLangs();

        if (!$this->getRequest()->isPost()) {
            return;
        } else {
            if (!$form->isValid($_POST)) {
                $this->view->form = $form;
                return;
            } else {
                $values = $form->getValues();
                $category = new CMS_Models_ContentCategory();
                $category = $category->createRow();
                $category->enabled = $values['common']['enabled'];
                $category->save();
                $langs = new CMS_Models_Lang();
                $langs = $langs->getEnabledLangs();
                foreach ($langs as $l) {
                    $langCategory = new CMS_Models_LangContentCategory();
                    $langCategory = $langCategory->createRow();
                    $langCategory->id_category = $category->id;
                    $langCategory->lang = $l['prefix2'];
                    $langCategory->name = $values[$l['prefix2']]['name_' . $l['prefix2']];
                    $langCategory->save();
                }

                $this->_redirect("/admin/content");
            }
        }
    }

    public function deletecategoryAction() {
        $this->disableView();
        $request = $this->_request->getParams();
        $category = $request['category'];
        $lc = new CMS_Models_LangContentCategory();
        $lc->delete("id_category = '$category'");
        $c = new CMS_Models_ContentCategory();
        $c = $c->getCategoryById($category);
        $c->delete();
        $this->_redirect("/admin/content");
    }

    private function addCategoryForm() {
        $form = new Zend_Form();
        $form->setAction("/admin/content/newcategory")
                ->setMethod('post');
        $form->setAttrib("class", "jNice");
        /*         * ** Datos comunes a los idiomas *** */
        $common = new Zend_Form_SubForm();
        $enabled = $common->createElement('checkbox', 'enabled');
        $enabled->setLabel('Activo:');
        $common->addElement($enabled);
        $form->addSubForms(array("common" => $common));

        $langs = new CMS_Models_Lang();
        $langs = $langs->getEnabledLangs();
        foreach ($langs as $l) {
            $sub = new Zend_Form_SubForm();
            $name = $sub->createElement("text", "name_" . $l['prefix2']);
            $name->setAttrib("class", "text-long");
            $name->addValidator('NotEmpty')->setLabel($this->view->t->_('*Nombre:'))->setRequired(true);
            $name->addErrorMessage($this->view->t->_('Este_campo_es_obligatorio'));
            $sub->addElement($name);
            $form->addSubForms(array($l['prefix2'] => $sub));
        }
        $form->addElement('submit', 'add', array('label' => $this->view->t->_('Aceptar')));
        return $form;
    }

    private function editCategoryForm() {
        $form = new Zend_Form();
        $form->setAction("/admin/content/editcategory")
                ->setMethod('post');
        $form->setAttrib("class", "jNice");
        $request = $this->_request->getParams();
        $category = $request['id_category'];
        $c = new CMS_Models_ContentCategory();
        $c = $c->getCategoryById($category);
        /*         * *** Datos comunes a todos los idiomas *** */
        $common = new Zend_Form_SubForm();
        $enabled = $common->createElement('checkbox', 'enabled');
        $enabled->setLabel('Activo:');
        $enabled->setValue($c->enabled);
        $common->addElement($enabled);
        $form->addSubForms(array("common" => $common));
        /*         * ** Datos por idioma **** */
        $langs = new CMS_Models_Lang();
        $langs = $langs->getEnabledLangs();
        foreach ($langs as $l) {
            $sub = new Zend_Form_SubForm();
            $langCategory = new CMS_Models_LangContentCategory();
            $langCategory = $langCategory->getCategoryByKey($c->id, $l['prefix2']);
            $name = $sub->createElement("text", "name_" . $l['prefix2']);
            $name->setAttrib("class", "text-long");
            $name->addValidator('NotEmpty')->setLabel($this->view->t->_('*Nombre:'))->setRequired(true);
            $name->addErrorMessage($this->view->t->_('Este_campo_es_obligatorio'));
            $name->setValue($langCategory['name']);
            $sub->addElement($name);
            $form->addSubForms(array($l['prefix2'] => $sub));
        }
        /*         * ************************ */
        $id = $form->createElement("hidden", 'id_category');
        $id->setValue($c->id);
        $form->addElement($id);
        $form->addElement('submit', 'add', array('label' => $this->view->t->_('Aceptar')));
        return $form;
    }

    public function editcategoryAction() {
        $form = $this->editCategoryForm();
        $this->view->form = $form;
        $langs = new CMS_Models_Lang();
        $this->view->langs = $langs->getEnabledLangs();

        if (!$this->getRequest()->isPost()) {
            return;
        } else {
            if (!$form->isValid($_POST)) {
                $this->view->form = $form;
                return;
            } else {
                $values = $form->getValues();
                var_dump($values);
                $category = new CMS_Models_ContentCategory();
                $category = $category->getCategoryById($values['id_category']);
                $category->enabled = $values['common']['enabled'];
                $category->save();
                $langs = new CMS_Models_Lang();
                $langs = $langs->getEnabledLangs();
                foreach ($langs as $l) {
                    $langCategory = new CMS_Models_LangContentCategory();
                    $langCategory = $langCategory->getCategoryById($values['id_category'], $l['prefix2']);

                    if (count($langCategory) <= 0) {
                        $langCategory = new CMS_Models_LangContentCategory();
                        $langCategory = $langCategory->createRow();
                    }
                    $langCategory->id_category = $category->id;
                    $langCategory->lang = $l['prefix2'];
                    $langCategory->name = $values[$l['prefix2']]['name_' . $l['prefix2']];
                    $langCategory->save();
                }
                $this->_redirect("/admin/content");
            }
        }
    }

    public function listcontentsAction() {
        $request = $this->_request->getParams();
        $category = $request['category'];
        $this->view->category = $category;
        $lang = Common_Translate::getLanguage();
        $c = new CMS_Models_LangContentCategory();
        $c = $c->getCategoryById($category, $lang);
        $this->view->categoryName = $c->name;
        $content = new CMS_Models_LangContent();
        $this->view->contents = $content->getContentsByCategory($category, $lang);
    }

    public function newcontentAction() {
        $form = $this->addContentForm();
        $this->view->form = $form;
        $langs = new CMS_Models_Lang();
        $this->view->langs = $langs->getEnabledLangs();
        if (!$this->getRequest()->isPost()) {
            return;
        } else {
            if (!$form->isValid($_POST)) {
                $this->view->form = $form;
                return;
            } else {
                $values = $form->getValues();

                $content = new CMS_Models_Content();
                $content = $content->createRow();
                $content->id_category = $values['common']['category'];
                $content->enabled = $values['common']['enabled'];
                $content->save();
                $langs = new CMS_Models_Lang();
                $langs = $langs->getEnabledLangs();
                foreach ($langs as $l) {
                    $langContent = new CMS_Models_LangContent();
                    $langContent = $langContent->createRow();
                    $langContent->id_content = $content->id;
                    $langContent->lang = $l['prefix2'];
                    $langContent->title = $values[$l['prefix2']]['title_' . $l['prefix2']];
                    $langContent->content = $values[$l['prefix2']]['description_' . $l['prefix2']];
                    $langContent->save();
                }
                $this->_redirect("/admin/content/listcontents/category/" . $values['common']['category']);
            }
        }
    }

    private function addContentForm() {
        $form = new Zend_Form();
        $form->setAction("/admin/content/newcontent")
                ->setMethod('post');
        $form->setAttrib("class", "jNice");
        $lang = Common_Translate::getLanguage();
        $id_content = $this->_request->getParam('content');
        /*         * ** Datos comunes *** */
        $common = new Zend_Form_SubForm();
        $category = $form->createElement("select", "category");
        $categories = new CMS_Models_LangContentCategory();
        $categories = $categories->getCategoriesByLang($lang);
        $category->setLabel('Categoría:');
        $category->addMultiOptions($this->formatCategories($categories));
        $enabled = $common->createElement('checkbox', 'enabled');
        $enabled->setLabel('Activo:');
        $common->addElement($category);
        $common->addElement($enabled);
        $form->addSubForms(array("common" => $common));
        /*         * ** Fin datos comunes ** */
        /*         * ** Datos por idioma *** */
        $langs = new CMS_Models_Lang();
        $langs = $langs->getEnabledLangs();
        foreach ($langs as $l) {
            $sub = new Zend_Form_SubForm();
            $name = $sub->createElement("text", "title_" . $l['prefix2']);
            $name->setAttrib("class", "text-long");
            $name->addValidator('NotEmpty')->setLabel($this->view->t->_('*Titulo:'))->setRequired(true);
            $name->addErrorMessage($this->view->t->_('Este_campo_es_obligatorio'));
            $sub->addElement($name);
            $description = $sub->createElement("textarea", "description_" . $l['prefix2']);
            $description->setAttrib("class", "mceAdvanced");
            $description->setLabel($this->view->t->_('Descripcion:'));
            $sub->addElement($description);
            $form->addSubForms(array($l['prefix2'] => $sub));
        }

        $form->addElement('submit', 'add', array('label' => 'Aceptar'));
        return $form;
    }

    private function formatCategories($categories) {
        $options = array();

        foreach ($categories as $c) {
            $options[$c['id']] = $c['name'];
        }
        return $options;
    }

    public function editcontentAction() {
        $form = $this->editContentForm();
        $this->view->form = $form;
        $langs = new CMS_Models_Lang();
        $this->view->langs = $langs->getEnabledLangs();
        if (!$this->getRequest()->isPost()) {
            return;
        } else {
            if (!$form->isValid($_POST)) {
                $this->view->form = $form;
                return;
            } else {
                $values = $form->getValues();
                $content = new CMS_Models_Content();
                $content = $content->getContentById($values['id_content']);
                $content->enabled = $values['common']['enabled'];
                $content->id_category = $values['common']['category'];
                $content->save();
                $langs = new CMS_Models_Lang();
                $langs = $langs->getEnabledLangs();
                foreach ($langs as $l) {
                    $langContent = new CMS_Models_LangContent();
                    $langContent = $langContent->getContentById($content->id, $l['prefix2']);
                    if (count($langContent) <= 0) {
                        $langContent = new CMS_Models_LangContent();
                        $langContent = $langContent->createRow();
                    }
                    $langContent->id_content = $values['id_content'];
                    $langContent->lang = $l['prefix2'];
                    $langContent->title = $values[$l['prefix2']]['title_' . $l['prefix2']];
                    $langContent->content = $values[$l['prefix2']]['description_' . $l['prefix2']];
                    $langContent->save();
                }
                $this->_redirect("/admin/content/listcontents/category/" . $values['common']['category']);
            }
        }
    }

    private function editContentForm() {
        $form = new Zend_Form();
        $form->setAction("/admin/content/editcontent")
                ->setMethod('post');
        $form->setAttrib("class", "jNice");
        $lang = Common_Translate::getLanguage();
        $id_content = $this->_request->getParam('id_content');

        $content = new CMS_Models_Content();
        $content = $content->getContentById($id_content);
        /*         * ** Datos comunes *** */
        $common = new Zend_Form_SubForm();
        $category = $form->createElement("select", "category");
        $categories = new CMS_Models_LangContentCategory();
        $categories = $categories->getCategoriesByLang($lang);
        $category->setLabel('Categoría:');
        $category->addMultiOptions($this->formatCategories($categories));
        $category->setValue($content->id_category);
        $enabled = $common->createElement('checkbox', 'enabled');
        $enabled->setLabel('Activo:');
        $enabled->setValue($content->enabled);
        $common->addElement($category);
        $common->addElement($enabled);
        $form->addSubForms(array("common" => $common));
        /*         * ** Fin datos comunes ** */
        /*         * ** Datos por idioma *** */
        $langs = new CMS_Models_Lang();
        $langs = $langs->getEnabledLangs();
        foreach ($langs as $l) {
            $sub = new Zend_Form_SubForm();
            $langContent = new CMS_Models_LangContent();
            $langContent = $langContent->getContentByKey($content->id, $l['prefix2']);
            $name = $sub->createElement("text", "title_" . $l['prefix2']);
            $name->setAttrib("class", "text-long");
            $name->addValidator('NotEmpty')->setLabel($this->view->t->_('*Titulo:'))->setRequired(true);
            $name->addErrorMessage($this->view->t->_('Este_campo_es_obligatorio'));
            $name->setValue($langContent->title);
            $sub->addElement($name);
            $description = $sub->createElement("textarea", "description_" . $l['prefix2']);
            $description->setAttrib("class", "mceAdvanced");
            $description->setLabel($this->view->t->_('Descripcion:'));
            $description->setValue($langContent['content']);
            $sub->addElement($description);
            $form->addSubForms(array($l['prefix2'] => $sub));
        }
        $id = $form->createElement("hidden", "id_content");
        $id->setValue($content->id);
        $form->addElement($id);
        $form->addElement('submit', 'add', array('label' => 'Aceptar'));
        return $form;
    }

    public function deletecontentAction() {
        $this->disableView();
        $request = $this->_request->getParams();
        $content = $request['content'];
        $category = $request['category'];
        $lc = new CMS_Models_LangContent();
        $lc->delete("id_content = '$content'");
        $c = new CMS_Models_Content();
        $c = $c->getContentById($content);
        $c->delete();
        $this->_redirect("/admin/content/listcontents/category/$category");
    }

}

